package SysAccountController

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	dto2 "project2/dto"
	"project2/g"
	models "project2/model"
	"project2/services/SysAccountService"
	"project2/utils"
	"strings"
)

func InitRoutes(r *gin.Engine) {
	r.POST("/api/sys_account/list", list)
	r.POST("/api/sys_account/add", add)
	r.POST("/api/sys_account/update", update)
	r.GET("/api/sys_account/delbyid", delbyid)
	r.GET("/api/sys_account/getbyid", getbyid)

	r.POST("/api/auth/login", authLogin)
	r.GET("/api/auth/getuserinfobyauth", getuserinfobyauth)

}
func list(ctx *gin.Context) {
	var body map[string]interface{}
	utils.GetAllBody(ctx, &body)
	var dto dto2.SysAccountDto
	dto2.BuildSysAccountDto(&body, &dto)

	var listdata []models.SysAccount
	var query *gorm.DB
	var total int64
	dto.BuildQuery(&query)
	query.Find(&listdata)
	query.Count(&total)

	utils.Ok(ctx, gin.H{
		"data":  listdata,
		"total": total,
	})

}

func add(ctx *gin.Context) {
	var body map[string]interface{}
	utils.GetAllBody(ctx, &body)
	var dto dto2.SysAccountDto
	dto2.BuildSysAccountDto(&body, &dto)

	var entity models.SysAccount
	dto.BuildEntityForAdd(&entity)
	g.Db.Create(&entity)
	utils.Ok(ctx, &entity)
}

func update(ctx *gin.Context) {
	var body map[string]interface{}
	utils.GetAllBody(ctx, &body)

	var dto dto2.SysAccountDto
	dto2.BuildSysAccountDto(&body, &dto)

	var entity models.SysAccount
	dto.BuildEntityForUpdate(&entity)

	var changeLog string
	entity.UpdateEnity(int(entity.Id), &changeLog)
	utils.Ok(ctx, &entity)
}

func delbyid(ctx *gin.Context) {
	id := utils.ParserInt8(ctx.Query("id"))
	if id <= 0 {
		utils.FailRes(ctx, "id 必填")
		return
	}
	g.Db.Where("id=?", id).Delete(&models.SysAccount{})
	utils.Ok(ctx, nil)

}
func getbyid(ctx *gin.Context) {
	id := utils.ParserInt8(ctx.Query("id"))
	if id <= 0 {
		utils.FailRes(ctx, "id 必填")
		return
	}
	var one models.SysAccount
	g.Db.Where("id=?", id).First(&one)
	if one.Id <= 0 {
		utils.Ok(ctx, nil)
		return
	}
	utils.Ok(ctx, one)

}

func authLogin(ctx *gin.Context) {

	var json = &map[string]interface{}{}
	ctx.Bind(json)

	/*查找用户 username 是否存在*/
	//utils.DD(str, 1)
	if (*json)["username"] == nil || (*json)["username"].(string) == "" {
		utils.FailRes(ctx, "username 不能为空")
		return
	}
	if (*json)["password"] == nil || (*json)["password"].(string) == "" {
		utils.FailRes(ctx, "password 不能为空")
		return
	}

	var user = &models.SysAccount{}
	//print(user)
	user.UserName = (*json)["username"].(string)
	user.PassWord = (*json)["password"].(string)

	var isyes = SysAccountService.
		ExitByUserName(user.UserName)
	if !isyes {
		utils.FailRes(ctx, fmt.Sprintf(":userName:%s不存在", user.UserName))
		return
	}

	var findUser = &models.SysAccount{}
	var isyes1, message = SysAccountService.FindUserOneByUserNameAndPassWord(findUser, user.UserName, user.PassWord)
	if !isyes1 {
		utils.FailRes(ctx, message)
		return
	}

	var token = SysAccountService.CreateToken()
	findUser.PassWord = ""
	utils.Ok(ctx, gin.H{
		"userInfo": findUser,
		"token":    token,
	})
}

func getuserinfobyauth(ctx *gin.Context) {
	var headerstr = ctx.GetHeader("Authorization")
	headerstr = strings.Replace(headerstr, "Bearer ", "", 1)
	print(headerstr)
}
